//
//  ZLEmojiToolbar.swift
//  Sina
//
//  Created by 朱玉龙 on 16/10/8.
//  Copyright © 2016年 朱玉龙. All rights reserved.
//

import UIKit

enum EmojiType: Int {
    case RECENT = 1
    case DEFAULT
    case EMOJI
    case LXH
    
}


class ZLEmojiToolbar: UIStackView {

    var ItemBlock: ((IndexPath) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        self.axis = .horizontal
        self.distribution = .fillEqually
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        addBtn(title: "最近", imageName: "compose_emotion_table_left", type: .RECENT)
        addBtn(title: "默认", imageName: "compose_emotion_table_mid", type: .DEFAULT)
        addBtn(title: "emoji", imageName: "compose_emotion_table_mid", type: .EMOJI)
        addBtn(title: "浪小花", imageName: "compose_emotion_table_right", type: .LXH)
        
    }
    
    private func addBtn(title: String,imageName: String, type: EmojiType) {
        
        let button = UIButton(title: title, titleColor: UIColor.white, fontSize: 17)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        button.setBackgroundImage(UIImage(named:imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named:imageName + "_selected"), for: .selected)
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(changeItem(btn:)), for: .touchUpInside)
        self.addArrangedSubview(button)
    }
    
    
    func changeItem(btn: UIButton) {
        
        let indexPath = IndexPath(item: 0, section: btn.tag - 1)
        
        self.selectedBtn.isSelected = false
        self.selectedBtn = btn
        btn.isSelected = true
        ItemBlock!(indexPath)
        
    }
   
    func  getChoiceItem(indexPath: IndexPath) {
        
        let btn = self.viewWithTag(indexPath.section + 1) as! UIButton
        
        if btn.isSelected == true {
            return
        }
        
        self.selectedBtn.isSelected = false
        self.selectedBtn = btn
        btn.isSelected = true
    }
    
    
    lazy var selectedBtn: UIButton = UIButton()
}
